package com.ljf.common.api.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ljf.common.domain.repository.IBasisRepository;
import com.ljf.common.domain.service.BasisDomainService;
import com.ljf.common.domain.validator.BasisValidator;
import com.ljf.common.dto.query.PageParam;
import com.ljf.common.exception.ServiceException;
import com.ljf.common.model.BasisModel;
import com.ljf.common.result.ServiceResultModel;
import com.ljf.common.service.IBasisService;
import com.ljf.common.utils.ServiceResultUtil;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 基础服务接口实现
 *
 * @author LJF
 * @since 2024-01-25
 */
public class BasisServiceImpl<D extends BasisDomainService<R, Q, V, M, A>, C extends BasisValidator<Q, A, V, M, R>, R extends IBasisRepository<Q, V, M>, Q extends PageParam, V extends Serializable, M extends BasisModel, A extends Serializable> extends NoDBBasisServiceImpl<D, C> implements IBasisService<Q, V, A> {

    /**
     * 分页查询接口
     *
     * @param query
     * @return
     * @throws ServiceException
     */
    @Override
    public ServiceResultModel<V> query(Q query) throws ServiceException {
        validator.checkQuery(query);
        Page<V> page = domainService.query(query);
        return ServiceResultUtil.success(page);
    }

    /**
     * 查询接口
     *
     * @param query
     * @return
     * @throws ServiceException
     */
    @Override
    public ServiceResultModel<V> queryList(Q query) throws ServiceException {
        validator.checkQueryList(query);
        List<V> page = domainService.queryList(query);
        return ServiceResultUtil.success(page);
    }

    /**
     * 更新接口
     *
     * @param command
     * @return
     * @throws ServiceException
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ServiceResultModel edit(A command) throws ServiceException {
        validator.checkEdit(command);
        return domainService.edit(command) ? ServiceResultUtil.success("更新成功") : ServiceResultUtil.fail("更新失败");
    }

    /**
     * 根据ID查询接口
     *
     * @param id
     * @return
     * @throws ServiceException
     */
    @Override
    public ServiceResultModel<V> queryById(Long id) throws ServiceException {
        validator.checkQueryById(id);
        V v = domainService.queryById(id);
        List<V> resultList = new ArrayList<>();
        resultList.add(v);
        return ServiceResultUtil.success(resultList);
    }

    /**
     * 查询接口(仅一条数据)
     *
     * @param query
     * @return
     * @throws ServiceException
     */
    @Override
    public ServiceResultModel<V> queryOne(Q query) throws ServiceException {
        validator.checkQueryOne(query);
        V v = domainService.queryOne(query);
        List<V> resultList = new ArrayList<>();
        resultList.add(v);
        return ServiceResultUtil.success(resultList);
    }

    /**
     * 查询接口(仅一条数据)
     *
     * @param query
     * @param noRecordMsg
     * @param multipleRecordMsg
     * @return
     * @throws ServiceException
     */
    @Override
    public ServiceResultModel<V> queryOne(Q query, String noRecordMsg, String multipleRecordMsg) throws ServiceException {
        validator.checkQueryOne(query);
        V v = domainService.queryOne(query, noRecordMsg, multipleRecordMsg);
        List<V> resultList = new ArrayList<>();
        resultList.add(v);
        return ServiceResultUtil.success(resultList);
    }

    /**
     * 新增接口
     *
     * @param command
     * @return
     * @throws ServiceException
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ServiceResultModel add(A command) throws ServiceException {
        validator.checkAdd(command);
        return domainService.add(command) != null ? ServiceResultUtil.success("新增成功") : ServiceResultUtil.fail("新增失败");
    }

    /**
     * 删除接口
     *
     * @param id
     * @return
     * @throws ServiceException
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ServiceResultModel delete(Long id) throws ServiceException {
        validator.checkDelete(id);
        return domainService.delete(id) ? ServiceResultUtil.success("删除成功") : ServiceResultUtil.fail("删除失败");
    }

    /**
     * 批量删除接口
     *
     * @param ids
     * @return
     * @throws ServiceException
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ServiceResultModel deleteBatch(List<Long> ids) throws ServiceException {
        validator.checkDeleteBatch(ids);
        return domainService.deleteBatch(ids) ? ServiceResultUtil.success("删除成功") : ServiceResultUtil.fail("删除失败");
    }

}
